package org.korosoft.findbugs.aggregator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileFilter;

public final class Crawler {
    private static final Log LOG = LogFactory.getLog(Crawler.class);
    private static final FileFilter DIR_FILTER = new FileFilter() {
        @Override
        public boolean accept(File pathName) {
            return pathName.isDirectory();
        }
    };

    private static final String FIND_BUGS_SUFFIX = File.separator + "findbugs.xml";

    private static final FileFilter FIND_BUGS_XML_FILTER = new FileFilter() {
        @Override
        public boolean accept(File pathName) {
            return pathName.getAbsolutePath().toLowerCase().endsWith(FIND_BUGS_SUFFIX);
        }
    };

    public void doCrawl(File projectDir, Callback callback) {
        LOG.debug("Processing directory " + projectDir.getAbsolutePath());
        for (File child : projectDir.listFiles(DIR_FILTER)) {
            doCrawl(child, callback);
        }
        for (File child : projectDir.listFiles(FIND_BUGS_XML_FILTER)) {
            LOG.info("Found file: " + child.getAbsolutePath());
            callback.process(child);
        }
    }

    public interface Callback {
        void process(File foundFile);
    }
}
